﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace LevelEditor
{
    public class ColorBox : Control
    {
        /// <summary>
        /// The color selected by the user
        /// </summary>
        public Color color = Color.Black;

        public event EventHandler ColorChanged;

        public ColorBox()
        {
            Click += ColorBox_Click;
            Paint += ColorBox_Paint;
        }

        void ColorBox_Click(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                color = dialog.Color;
                Invalidate();
                if (ColorChanged != null)
                    ColorChanged(this, new EventArgs());
            }
        }

        void ColorBox_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Graphics g = e.Graphics;

            Brush b = new SolidBrush(color);
            g.FillRectangle(b, ClientRectangle);
            g.DrawRectangle(Pens.Black, new Rectangle(0, 0, Width - 1, Height - 1));
        }
    }
}